//
//  TemplateViewController.m
//  FootPrint
//
//  Created by alex tao on 4/16/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "TemplateViewController.h"
#import "TemplateManager.h"
#import "UserGridCell.h"

@interface TemplateViewController ()

@end

@implementation TemplateViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _gridScroll.gridDelegate = self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self refresh];
}

- (void) refresh
{
    [_showTemplate release];
    _showTemplate = [[[TemplateManager sharedInstance] allTemplate] retain];
    NSMutableArray * views = [NSMutableArray arrayWithCapacity:_showTemplate.count];
    for (EventMessage * event in _showTemplate) {
        UserGridCell * cell = [[[UserGridCell alloc] initWithFrame:CGRectMake(0, 0, 80, 80)] autorelease];
        [cell updateEventMessage:event];
        [views addObject:cell];
    }
    
    _gridScroll.cells = views;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    _selDelegate = nil;
    [_selEvent release];
    [_showTemplate release];
    [_gridScroll release];
    [super dealloc];
}

- (void)viewDidUnload {
    [self setGridScroll:nil];
    [super viewDidUnload];
}

- (IBAction)exit:(id)sender {
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
}


#pragma mark - GridScrollDelegate

- (void) didSelect:(GridScrollView *)gridScroll atTotalIndex :(NSInteger)idx
{
    [_selEvent release];
    if (idx < _showTemplate.count)
    {
        _selEvent = [[_showTemplate objectAtIndex:idx] retain];
        
        UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                                 delegate:self
                                                        cancelButtonTitle:@"取消"
                                                   destructiveButtonTitle:nil
                                                        otherButtonTitles:@"选择", @"删除", nil];
        
        [actionSheet showInView:self.view];
        [actionSheet release];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString * title = [actionSheet buttonTitleAtIndex:buttonIndex];
    if ([title isEqualToString:@"选择"])
    {
        if ([_selDelegate didSelTemplate:_selEvent]) {
            [self exit:nil];
        }
    }
    else if ([title isEqualToString:@"删除"])
    {
        [[TemplateManager sharedInstance] removeTamplate:_selEvent.title];
        [self refresh];
    }
}

@end
